import { createSlice } from '@reduxjs/toolkit'
import axios from 'axios'

const channelStore = createSlice({
    name: 'channel',
    // 初始化状态 state
    initialState: {
        channelList: []
    },
    // 修改 state 的方法 同步方法 支持直接修改
    reducers: {
        setChannels(state, action) {
            state.channelList = action.payload
        }
    }
})


// 异步请求
export const fetchChannelList = () => {
    return async (dispatch: any) => {
        const res = await axios.get('http://geek.itheima.net/v1_0/channels')
        dispatch(setChannels(res.data.data.channels))
    }
}


// 解构 出来 actionCreator 函数
const { setChannels } = channelStore.actions

// 导出 reducer 默认导出
export default channelStore.reducer